var amqp = require('amqplib');
const MongoClient = require('mongodb').MongoClient;
const {sleep} = require('../util.js')
const {MONGO_URL,AMQP_ADDRESS,QUEUE_WK_NAME,QUEUE_RET_NAME,QUEUE_MAX_COUNT,MONGO_DB_NAME,MONGO_TABLE_NAME} = require('../config.js')
const fs = require('fs');

// DATA schema
// {
//   _id: name keywork
//   name:
//   keyword:
//   name_match:
//   keyword_match:
//   baidu_match:
// }

let KEY_WORDS = (()=>{
  let data = fs.readFileSync('./data/n.txt','utf8').split('\n')
  let tmp_set = {}
  let ret_array = []
  for (i in data){
    tmp_set[data[i].trim()] = null
  }
  for (i in tmp_set){
    ret_array.push(i)
  }
  return ret_array
})();

// 获取所有公司名
let COM_NAMES = (()=>{
    let data = fs.readFileSync('./data/c.txt','utf8').split('\n')
    let tmp_set = {}
    let ret_array = []
    for (i in data){
      tmp_set[data[i].trim()] = null
    }
    for (i in tmp_set){
      ret_array.push(i)
    }
    return ret_array
})();



const product = async () => {
  let conn = await amqp.connect(AMQP_ADDRESS)
  let ch = await conn.createChannel()
  try{ch.deleteQueue(QUEUE_WK_NAME)} catch(err){console.log(err)}

  let q = await ch.assertQueue(QUEUE_WK_NAME, {durable: false});
  console.log(COM_NAMES.length)
  while(true){
    let client = await MongoClient.connect(MONGO_URL, {useNewUrlParser: true});
    let db = client.db(MONGO_DB_NAME);
    try {
      for (i in COM_NAMES){
        scount = 0
        for (j in KEY_WORDS){
          let search_key = '"'+COM_NAMES[i]+'" "'+KEY_WORDS[j]+'"'
          if (search_key.length<=40){
            scount += 1
          }
        }
        console.log(COM_NAMES[i])
        var count = await db.collection(MONGO_TABLE_NAME).countDocuments({'name': COM_NAMES[i]});
        console.log(count)
        if (count >= scount){
          continue
        }
        for (j in KEY_WORDS){
          let search_key = '"'+COM_NAMES[i]+'" "'+KEY_WORDS[j]+'"'
          if (search_key.length>40){
            console.log('lenght > 40',search_key)
            continue
          }
          if (count!=0){
            let doc = await db.collection(MONGO_TABLE_NAME).findOne({'_id': COM_NAMES[i] + ' ' + KEY_WORDS[j]});
            if (doc)
              continue
          }
          let data = {name:COM_NAMES[i].trim(),keyword:KEY_WORDS[j].trim()}
          ch.sendToQueue(q.queue,Buffer.from(JSON.stringify(data)));
        }
        // 队列暂停
        while (true){
          let aq = await ch.checkQueue(q.queue)
          if (aq.messageCount > QUEUE_MAX_COUNT){
            await sleep(5000)
            continue
          }
          break
        }
      }


    } catch (err) {
      if (err.name == "MongoError" && err.codeName == "CursorNotFound") {
        alert( "CursorNotFound" + e.message );
        client.close();
        continue
      } else {
        console.error(err); // rethrow (*)
      }
    } finally {
      // client.close();
      break
      client.close();
    }
  }
  conn.close()
}




product()
